    package com.imut.oa.service.impl;

    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.imut.oa.common.PageResult;
    import com.imut.oa.util.ThreadLoaclUtil;
    import com.imut.oa.mapper.LeaveMapper;

    import com.imut.oa.pojo.dto.LeavePageQueryDTO;
    import com.imut.oa.pojo.dto.LeaveSubmitDTO;
    import com.imut.oa.pojo.dto.CommonUserPageQueryDTO;
    import com.imut.oa.pojo.dto.UpdateLeaveStatusDTO;
    import com.imut.oa.pojo.entity.Leave;
    import com.imut.oa.pojo.vo.LeaveVO;
    import com.imut.oa.service.LeaveService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.time.temporal.ChronoUnit;

    @Service
    @Slf4j
    public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave>
            implements LeaveService {
        @Autowired
        private LeaveMapper leaveMapper;
        @Override
        public PageResult getLeavePage(LeavePageQueryDTO leavePageQueryDTO) {
            // 创建条件对象，不设置任何条件，相当于查询所有
            Page<LeaveVO> page = new Page<>(leavePageQueryDTO.getPage(), leavePageQueryDTO.getPageSize());
            // 调用自定义的联表分页查询
            Page<LeaveVO> voPage = leaveMapper.selectLeavePage(page, leavePageQueryDTO);
            System.out.println(voPage);
            return new PageResult(voPage.getTotal(), voPage.getRecords());
        }

        @Override
        public void updateStatus(UpdateLeaveStatusDTO updateLeaveStatusDTO) {
            Leave build = Leave.builder()
                    .id(updateLeaveStatusDTO.getId())
                    .status(updateLeaveStatusDTO.getStatus())
                    .approveComment(updateLeaveStatusDTO.getApproveComment())
                    .build();
            leaveMapper.updateLeave(build);
        }
        @Override
        @Transactional
        public void submitLeave(LeaveSubmitDTO submitDTO) {
            Long currentId = ThreadLoaclUtil.getCurrentId();
            log.info("获取当前登录用户信息：{}",currentId);
            // 检查当天是否有该用户处于审核中的请假记录
            if (leaveMapper.existsPendingLeaveByUserAndDate(currentId, submitDTO.getStartDate())) {
                throw new RuntimeException("请假已申请，审核中...");
            }
            if (submitDTO == null) {
                throw new RuntimeException("请假信息不能为空");
            }
            Leave leave = new Leave();
            leave.setDuration(
                    (int) ChronoUnit.DAYS.between(
                            submitDTO.getStartDate(),
                            submitDTO.getEndDate()
                    ) + 1
            );
            // 这里需要获取当前登录用户ID
            leave.setEmployeeId(currentId);
            leave.setLeaveType(submitDTO.getLeaveType());
            leave.setStartTime(submitDTO.getStartDate().atStartOfDay());  // 2025-04-28 00:00:00
            leave.setEndTime(submitDTO.getEndDate().atTime(23, 59, 59));  // 2025-04-28 23:59:59
            leave.setReason(submitDTO.getReason());
            leave.setStatus(0); // 初始状态为待审批
            leaveMapper.insertLeave(leave);
        }
        @Override
        public PageResult getLeaveHistory(CommonUserPageQueryDTO commonUserPageQueryDTO) {
            Page<LeaveVO> page = new Page<>(commonUserPageQueryDTO.getPage(), commonUserPageQueryDTO.getPageSize());
            Page<LeaveVO> voPage = leaveMapper.selectUserLeavePage(page, commonUserPageQueryDTO);
            return new PageResult(voPage.getTotal(), voPage.getRecords());
        }

        @Override
        public void cancelLeave(Long leaveId) {
            Leave leave = new Leave();
            leave.setId(leaveId);
            leave.setStatus(3); // 3 表示已撤销
            leaveMapper.updateLeave(leave);
        }
        @Override
        public boolean checkIsLeaveToday(Long employeeId, String day) {
            return leaveMapper.checkIsLeaveToday(employeeId, day);
        }
    }
